﻿using System;

namespace ConsoleAppFramework.Util
{
    public static class FormatHelper
    {
        public static string ImgSuffix(this string img)
        {
            return img.Replace(".webp-thumb", ".jpg").Replace(".webp", ".jpg");
        }
        public static double AmountFormat(double amount)
        {
            return Math.Round((double)amount / 100, 2);
        }
        public static string DurationSecondsFormat(int seconds)
        {
            if (seconds <= 0)
            {
                return "--";
            }
            TimeSpan ts = new TimeSpan(0, 0, seconds);
            string str = "";
            if (ts.Hours > 0)
            {
                str = ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分" + ts.Seconds + "秒";
            }
            if (ts.Hours == 0 && ts.Minutes > 0)
            {
                str = ts.Minutes.ToString() + "分" + ts.Seconds + "秒";
            }
            if (ts.Hours == 0 && ts.Minutes == 0)
            {
                str = ts.Seconds + "秒";
            }
            return str;
        }
        public static string NullableToDescribe(this decimal? value)
        {
            if (!value.HasValue)
            {
                return "--";
            }
            return value.Value.ToNumberDescribe();
        }
        public static string NullableToDescribe(this int? value)
        {
            if (!value.HasValue)
            {
                return "--";
            }
            return value.Value.ToNumberDescribe();
        }
        public static string NullableToDescribe(this double? value)
        {
            if (!value.HasValue)
            {
                return "--";
            }
            return value.Value.ToNumberDescribe();
        }
        public static string NullableToDescribe(this long? value)
        {
            if (!value.HasValue)
            {
                return "--";
            }
            return value.Value.ToNumberDescribe();
        }
        public static string NullableToDescribe(this float? value)
        {
            if (!value.HasValue)
            {
                return "--";
            }
            return value.Value.ToNumberDescribe();
        }
        public static string ToNumberDescribe(this long d)
        {
            var prefix = "";
            if (d < 0)
            {
                prefix = "-";
            }

            if (Math.Abs(d) >= 10000 && Math.Abs(d) < 100000000)
            {
                return prefix + FormatMoney(CutDecimalWithN((Math.Abs(d * 1.0) / 10000), 1).ToString("0.##")) + "w";
            }
            else if (Math.Abs(d) >= 100000000)
            {
                return prefix + FormatMoney(CutDecimalWithN((Math.Abs(d * 1.0) / 100000000), 1).ToString("0.##")) + "亿";
            }
            else
            {
                return d.ToString("N0");
            }
        }
        public static string ToNumberDescribe(this double d, string keepdecimal = "0.#")
        {
            var prefix = "";
            if (d < 0)
            {
                prefix = "-";
            }
            if (Math.Abs(d) >= 10000 && Math.Abs(d) < 100000000)
            {
                return prefix + FormatMoney(CutDecimalWithN((Math.Abs(d) / 10000), 1).ToString("0.##")) + "w";
            }
            else if (Math.Abs(d) >= 100000000)
            {
                return prefix + FormatMoney(CutDecimalWithN((Math.Abs(d * 1.0) / 100000000), 1).ToString("0.##")) + "亿";
            }
            else
            {
                return d.ToString(keepdecimal);
            }
        }
        public static string ToNumberDescribe(this int d)
        {
            var prefix = "";
            if (d < 0)
            {
                prefix = "-";
            }

            if (Math.Abs(d) >= 10000 && Math.Abs(d) < 100000000)
            {
                return prefix + FormatMoney(CutDecimalWithN((Math.Abs(d) * 1.0 / 10000), 1).ToString("0.##")) + "w";
            }
            else if (Math.Abs(d) >= 100000000)
            {
                return prefix + FormatMoney(CutDecimalWithN((Math.Abs(d * 1.0) / 100000000), 1).ToString("0.##")) + "亿";
            }
            else
            {
                return d.ToString("N0");
            }
        }
        public static string ToNumberDescribe(this decimal d)
        {
            var prefix = "";
            if (d < 0)
            {
                prefix = "-";
            }

            if (Math.Abs(d) >= 10000 && Math.Abs(d) < 100000000)
            {
                return prefix + FormatMoney(CutDecimalWithN((d / 10000), 1).ToString("0.##")) + "w";
            }
            else if (Math.Abs(d) >= 100000000)
            {
                return prefix + FormatMoney(CutDecimalWithN((d / 100000000), 1).ToString("0.##")) + "亿";
            }
            else
            {
                return d.ToString("N0");
            }
        }
        public static string ToNumberDescribe(this float d, string keepdecimal = "N0")
        {
            var prefix = "";
            if (d < 0)
            {
                prefix = "-";
            }
            if (Math.Abs(d) >= 10000)
            {
                return prefix + FormatMoney(CutDecimalWithN((Math.Abs(d) / 10000), 1).ToString("0.##")) + "w";
            }
            else
            {
                return d.ToString(keepdecimal);
            }
        }
        public static string ToZeroNumberDescribe(this int d)
        {
            if (d == 0)
            {
                return "--";
            }
            var prefix = "";
            if (d < 0)
            {
                prefix = "-";
            }

            if (Math.Abs(d) >= 10000)
            {
                return prefix + (Math.Abs(d) * 1.0 / 10000).ToString("f1") + "w";
            }
            else
            {
                return d.ToString("f0");
            }
        }
        public static string ToZeroOmitNumberDescribe(this long d)
        {
            if (d == 0)
            {
                return "--";
            }
            var prefix = "";
            if (d < 0)
            {
                prefix = "-";
            }
            if (Math.Abs(d) >= 1000 && Math.Abs(d) < 10000)
            {
                return prefix + (Math.Abs(d) * 1.0 / 1000).ToString("f0") + "k";
            }
            else if (Math.Abs(d) >= 10000 && Math.Abs(d) < 100000000)
            {
                return prefix + (Math.Abs(d) * 1.0 / 10000).ToString("f1") + "w";
            }
            else if (Math.Abs(d) >= 100000000)
            {
                return prefix + (Math.Abs(d) * 1.0 / 100000000).ToString("f1") + "亿";
            }
            else
            {
                return d.ToString("f0");
            }
        }
        /// <summary>
        /// 对数字添加”,“号，可以处理负数以及带有小数的情况
        /// </summary>
        /// <param name="version"></param>
        /// <returns></returns>
        public static string FormatMoney(string money)
        {
            //处理带有负号情况
            int negNumber = money.IndexOf("-");
            string prefix = string.Empty;
            if (negNumber != -1)
            {
                prefix = "-";
                money = money.Substring(1);
            }
            //处理有小数位情况
            int decNumber = money.IndexOf(".");
            string postfix = string.Empty;
            if (decNumber != -1)
            {
                postfix = money.Substring(decNumber);
                money = money.Substring(0, decNumber);
            }
            //开始添加”,“号
            if (money.Length > 3)
            {
                string str1 = money.Substring(0, money.Length - 3);
                string str2 = money.Substring(money.Length - 3, 3);
                if (str1.Length > 3)
                {
                    return prefix + FormatMoney(str1) + "," + str2 + postfix;
                }
                else
                {
                    return prefix + str1 + "," + str2 + postfix;
                }
            }
            else
            {
                return prefix + money + postfix;
            }
        }
        public static double CutDecimalWithN(double d, int n)
        {
            string strDecimal = d.ToString();
            int index = strDecimal.IndexOf(".");
            if (index == -1 || strDecimal.Length < index + n + 1)
            {
                strDecimal = string.Format("{0:F" + n + "}", d);
            }
            else
            {
                int length = index;
                if (n != 0)
                {
                    length = index + n + 1;
                }
                strDecimal = strDecimal.Substring(0, length);
            }
            return Double.Parse(strDecimal);
        }
        public static double CutDecimalWithN(decimal d, int n)
        {
            string strDecimal = d.ToString();
            int index = strDecimal.IndexOf(".");
            if (index == -1 || strDecimal.Length < index + n + 1)
            {
                strDecimal = string.Format("{0:F" + n + "}", d);
            }
            else
            {
                int length = index;
                if (n != 0)
                {
                    length = index + n + 1;
                }
                strDecimal = strDecimal.Substring(0, length);
            }
            return Double.Parse(strDecimal);
        }
    }
}
